package com.ai.controller.made;


import com.ai.annotation.EnterPathParamLog;
import com.ai.common.CommonResponse;
import com.ai.entity.made.PlcLogs;
import com.ai.entity.made.page.TPlcLogsPage;
import com.ai.model.param.PlcLogsParams;
import com.ai.service.made.IPlcLogsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * PLC日志记录 前端控制器
 * </p>
 *
 * @author jiaok
 * @since 2023-04-18 10:42:26
 */
@RestController
@RequestMapping("/plc-logs")
@Slf4j
public class PlcLogsController {


    @Resource
    private IPlcLogsService iPlcLogsService;


    @ApiOperation("分页查询")
    @PostMapping("list")
    @EnterPathParamLog
    public IPage<PlcLogs> list(@RequestBody TPlcLogsPage tPlcLogsPage) {
//        QueryWrapper<Slabs> queryWrapper = slabsPages.getQuery(new QueryWrapper<>());
//        IPage<Slabs> iPage = slabsService.page(slabsPages, queryWrapper);
        QueryWrapper<PlcLogs> query = tPlcLogsPage.getQuery(new QueryWrapper<>());
        TPlcLogsPage page = iPlcLogsService.page(tPlcLogsPage, query);
        return page;
    }


    @ApiOperation("查询指定日志")
        @PostMapping("selectById/{id}")
    public CommonResponse<PlcLogs> queryByLogId(@PathVariable(name = "id") Integer id) {
       return iPlcLogsService.queryByLogId(id);
    }



    @ApiOperation("新增plc日志")
    @PostMapping("add")
    @EnterPathParamLog
    public CommonResponse<String> add(@RequestBody @Validated PlcLogsParams plcLogsParams){
       return iPlcLogsService.add(plcLogsParams);
    }

    @ApiOperation("删除plc日志")
    @PostMapping("delete/{id}")
    @EnterPathParamLog
    public CommonResponse<String> delete(@PathVariable(name = "id") Integer id){
        boolean b = iPlcLogsService.removeById(id);
        if (b){
            return CommonResponse.ok("删除成功");
        }else {
            return CommonResponse.error("删除失败");
        }

    }

    @ApiOperation("更新plc日志")
    @PostMapping("update")
    @EnterPathParamLog
    public CommonResponse<String> update(@RequestBody @Validated PlcLogsParams plcLogsParams){
        if (StringUtils.isNotBlank(plcLogsParams.getId())){
            return iPlcLogsService.updatePlcBlog(plcLogsParams);
        }else {
            return CommonResponse.error("plc日志id不能为空");
        }
    }

    /*
     {
         "request" : {
            "createTime" : "2023-04-19 00:00:00",
            "functionType" : 1,
            "messageContent" : "DB1",
            "switchStatus" : 0
         },
         "type" : "101010101010"
      }
     */

//    @ApiOperation("更新plc日志")
//    @PostMapping("test")
//    public CommonResponse<String> test(){
//        String jsonMessage="{\n" +
//                "         \"request\" : {\n" +
//                "            \"createTime\" : \"2023-04-19 00:00:00\",\n" +
//                "            \"functionType\" : 1,\n" +
//                "            \"messageContent\" : \"DB1\",\n" +
//                "            \"switchStatus\" : 0\n" +
//                "         },\n" +
//                "         \"type\" : \"/plc/log\"\n" +
//                "      }";
////        String jsonMessage=  "        {         'request' : {            'createTime' : '2023-04-19 00:00:00',            'functionType' : 1,            'messageContent' : 'DB1',            'switchStatus' : 0         },         'type' : '/plc/log'      }";
////        jsonMessage= jsonMessage.replace("\n", "");
////        jsonMessage = jsonMessage.replace("\"", "'");
//
//        JSONObject o = JSONObject.parseObject(jsonMessage);
//        String s =   o.getString("type");
//
//        int i = jsonMessage.indexOf("/plc/log");
//        String type = jsonMessage.substring(jsonMessage.lastIndexOf("type"), jsonMessage.lastIndexOf("}"));
//        log.debug(type);
//        //        String message = jsonMessage.substring(1, jsonMessage.lastIndexOf(","));
////        String substring = message.substring(message.indexOf("{")-1, message.lastIndexOf("}")+1);
////        PlcLogs plcLogs = JSONObject.parseObject(substring, PlcLogs.class);
//        return null;
//    }
}

